﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using AFS.VisitationRequestTool.Addin.Common;
using System.Windows.Forms;
using AFS.VisitationRequestTool.Addin.Entities;
using AFS.VisitationRequestTool.Addin.Components;
using AFS.VisitationRequestTool.Addin.Dictionary;
using System.Runtime.InteropServices;
using AFS.VisitationRequestTool.Addin.Variables;

namespace AFS.VisitationRequestTool.Addin.Wrappers
{
    /// <summary>
    /// We derive a Wrapper for each MessageClass / ItemType
    /// </summary>
    public class MeetingItemWrapper : InspectorWrapper
    {
        /// <summary>
        /// Gets the Object instance behind the Inspector (CurrentItem)
        /// </summary>
        public MeetingItem meetingItem { get; private set; }

        /// <summary>
        /// Gets the Object instance holding the controller behind form/ribbon interactions
        /// </summary>
        private VisitationRequestPresenter vrPresenter;

        public VisitationRequestPresenter VrPresenter { get; private set; }

        /// <summary>
        /// .ctor
        /// </summary>
        /// <param name="inspector">The Outlook Inspector instance that should be handled</param>
        public MeetingItemWrapper(Inspector inspector)
            : base(inspector)
        {
        }

        /// <summary>
        /// Method is called when the Wrapper has been initialized
        /// </summary>
        protected override void Initialize()
        {
            // Get the Item of the current Inspector
            meetingItem = (MeetingItem)Inspector.CurrentItem;

            // Register for the Item events
            meetingItem.BeforeRead += new ItemEvents_10_BeforeReadEventHandler(Item_BeforeRead);

            meetingItem.Open += new ItemEvents_10_OpenEventHandler(Item_Open);
            meetingItem.Write += new ItemEvents_10_WriteEventHandler(Item_Write);
            meetingItem.AfterWrite += new ItemEvents_10_AfterWriteEventHandler(Item_AfterWrite);

            meetingItem.Application.ItemSend += new ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
            meetingItem.Application.ItemLoad += new ApplicationEvents_11_ItemLoadEventHandler(Application_ItemLoad);     
        }

        void Application_ItemLoad(object Item)
        {
            vrPresenter = new VisitationRequestPresenter();
        }

        void Application_ItemSend(object Item, ref bool Cancel)
        {
            //Required
            Cancel = vrPresenter.CancelInvite(meetingItem);
        }

        /// <summary>
        /// Method gets called when the Inspector is activated
        /// </summary>
        protected override void Activate()
        {
            vrPresenter = new VisitationRequestPresenter();
        }

        void Item_BeforeRead() { }
        void Item_AfterWrite() { }

        /// <summary>
        /// This Method is called when the Item is visible and the UI is initialized.
        /// </summary>
        /// <param name="Cancel">When you set this property to true, the Inspector is closed.</param>
        void Item_Open(ref bool Cancel) 
        {
           
        }

        /// <summary>
        /// This Method is called when the Item is saved.
        /// </summary>
        /// <param name="Cancel">When set to true, the save operation is cancelled</param>
        ///
        void Item_Write(ref bool Cancel)
        {
            vrPresenter.SaveItemDetails();
        }     

        /// <summary>
        /// The Close Method is called when the Inspector has been closed.
        /// Do your cleanup tasks here.
        /// The UI is gone, can't access it here.
        /// </summary>
        protected override void Close()
        {
            // unregister events
            meetingItem.Write -= new ItemEvents_10_WriteEventHandler(Item_Write);
            meetingItem.Open -= new ItemEvents_10_OpenEventHandler(Item_Open);

            // Release references to COM objects
            meetingItem = null;

            // required, just stting to NULL may keep a reference in memory of the Garbage Collector.
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}
